/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jturbofile.gui;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Rolf
 */
public class TrayGUI {
    private static TrayGUI singleton;
    private TrayIcon trayIcon;
    
    public static TrayGUI singleton() {
        if( singleton == null) {
            singleton = new TrayGUI();
        }
        
        return singleton;
    }
    
    private TrayGUI() {
        if(SystemTray.isSupported()) {
            //NetCore core = new NetCore(6060);
            SystemTray tray = SystemTray.getSystemTray();
            
            Image image = Toolkit.getDefaultToolkit().getImage(TrayGUI.class.getResource("jturbofile.png"));

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    LogPanel.singleton().logln("Exiting...");
                    System.exit(0);
                }
            };
            
            ActionListener showGUIListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JTurboFileGUI.singleton().setVisible(true);
                }
            };

            PopupMenu popup = new PopupMenu();
            MenuItem showGUIItem = new MenuItem("Show GUI");
            showGUIItem.addActionListener(showGUIListener);
            popup.add(showGUIItem);

            MenuItem exitItem = new MenuItem("Exit");
            exitItem.addActionListener(exitListener);
            popup.add(exitItem);
            
            trayIcon = new TrayIcon(image, "JTurboFile", popup);
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(showGUIListener);
            
            try {
                tray.add(trayIcon);
                
                LogPanel.singleton().logln("TrayGUI started...");
            }
            catch(Exception e) {
                LogPanel.singleton().logln("Exception creating tray icon: "+e);
            }
        }
    }
    
    public void displayMessage(String caption, String msg, MessageType msgType) {
        trayIcon.displayMessage(caption, msg, msgType);
    }
}
